package com.xwsz.demo.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.xwsz.demo.utils.HttpClientUtil;
import com.xwsz.demo.utils.WordUtils;
import com.xwsz.demo.vo.RequestInfoVo;
import com.xwsz.demo.vo.RequestParamVo;

/**
 * 
 * @author Yanqing
 * 
 */
@Service
public class SwaggerJsonService {

	/**
	 * 通过swagger-api-docs地址获取文档json内容
	 * @param url
	 * @return
	 */
	public String getSwaggerJsonStr(String url) {
		String respStr = HttpClientUtil.sendHttpGet(url);
		if(StringUtils.isNotBlank(respStr)) {
			JSONObject swaggerJson = JSON.parseObject(respStr);
			JSONObject info = swaggerJson.getJSONObject("info");
			JSONArray tags = swaggerJson.getJSONArray("tags");
			JSONObject paths = swaggerJson.getJSONObject("paths");
			return respStr;
		}
		return "";
	}
	
	/**
	 * 获取接口请求信息
	 * @param paths
	 * @return
	 */
	public List<RequestInfoVo> getRequestInfo(JSONObject swaggerJson){
		JSONObject paths = swaggerJson.getJSONObject("paths");
		Map<String,JSONObject> refMap = getDefinitions(swaggerJson);
		List<RequestInfoVo> requestList = new ArrayList<>();
		Set<String> pathSet = paths.keySet();
		int i = 0;
		for(String path : pathSet) {
			RequestInfoVo info = new RequestInfoVo();
			info.setNumber((i+1)+"");
			i ++;
			JSONObject infoJson = paths.getJSONObject(path);
			info.setRequestUrl(path);
			Set<String> methodSet = infoJson.keySet();
			for(String method : methodSet) {
				info.setRequestMethod(method);
				JSONObject paramInfo = infoJson.getJSONObject(method);
				String summary = paramInfo.getString("summary");
				info.setDescription(summary);
				JSONArray parameterArr = paramInfo.getJSONArray("parameters");
				List<RequestParamVo> parameters = new ArrayList<>();
				if(!CollectionUtils.isEmpty(parameterArr)) {
					for(int j = 0; j < parameterArr.size(); j ++) {
						JSONObject paramJson = parameterArr.getJSONObject(j);
						RequestParamVo param = JSON.parseObject(JSON.toJSONString(paramJson),RequestParamVo.class);
						if(paramJson.containsKey("schema")) {
							JSONObject schema = paramJson.getJSONObject("schema");
							String ref = schema.getString("$ref");
							String def = ref.substring(14);
							JSONObject defJson = refMap.get(def);
							param.setType(defJson.getString("type"));
						}
						param.setSerial((j+1)+"");
						parameters.add(param);
					}
				}
				info.setParameters(parameters);
			}
			requestList.add(info);
		}
		return requestList;
	}
	
	public Map<String,JSONObject> getDefinitions(JSONObject swaggerJson){
		Map<String,JSONObject> map= new HashMap<>();
		JSONObject definitions = swaggerJson.getJSONObject("definitions");
		Set<String> definitionSet = definitions.keySet();
		for(String def : definitionSet) {
			map.put(def, definitions.getJSONObject(def));
		}
		return map;
	}
	
	public void wordExport(String url) {
		String respStr = getSwaggerJsonStr(url);
		if(StringUtils.isNotBlank(respStr)) {
			JSONObject swaggerJson = JSON.parseObject(respStr,Feature.DisableCircularReferenceDetect);
			List<RequestInfoVo> requestInfoList = getRequestInfo(swaggerJson);
			 Map<String, Object> dataMap = new HashMap<>();
			 dataMap.put("requestList", requestInfoList);
			 String filePath = "D:/doc_f/";
			 //文件唯一名称
		     String fileOnlyName = "API.doc";
		     /** 生成word  数据包装，模板名，文件生成路径，生成的文件名*/
		     WordUtils.createWord(dataMap, "template/swaggerToWord.ftl", filePath, fileOnlyName);
		}
	}
	
}
